package rwt.server.db;

import java.sql.*;

import org.apache.tomcat.jdbc.pool.*;

class DbConnectionPool
{
	private DataSource datasource;
	
	DbConnectionPool(String url, String driverClassName)
	{
		PoolProperties p = new PoolProperties();
	    p.setUrl(url);
	    p.setDriverClassName(driverClassName);
	    p.setJmxEnabled(true);
	    p.setTestWhileIdle(false);
	    p.setTestOnBorrow(true);
	    p.setValidationQuery("SELECT 1");
	    p.setTestOnReturn(false);
	    p.setValidationInterval(30000);
	    p.setTimeBetweenEvictionRunsMillis(30000);
	    p.setMaxActive(100);
	    p.setInitialSize(10);
	    p.setMaxWait(10000);
	    p.setRemoveAbandonedTimeout(60);
	    p.setMinEvictableIdleTimeMillis(30000);
	    p.setMinIdle(10);
	    p.setLogAbandoned(true);
	    p.setRemoveAbandoned(true);
	    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
	      "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
	    
	    datasource = new DataSource();
	    datasource.setPoolProperties(p);
	}
	
	Connection getConnection() throws SQLException
	{
		return datasource.getConnection();
	}
}
